<?php
//
// SourceForge: Breaking Down the Barriers to Open Source Development
// Copyright 1999-2000 (c) The SourceForge Crew
// http://sourceforge.net
//
// 


/**
 * return the file name that is part of the request_uri
 */
function viewvc_utils_getfile($script_name) {
  $request_uri = getStringFromServer('REQUEST_URI');
  $query_string = getStringFromServer('QUERY_STRING');

  $begin = strlen($script_name)+1;
  $length = strpos($request_uri,"?") - $begin;
  if ($length > 0) {
    $file = substr($request_uri,$begin,$length);
    //sort out magic paths like *checkout*, *docroot*
    if (strpos($file,"*") === 0) $file = substr($file, strpos($file,"*",1)+2);
  } else {
    $file = "";
  }
  return urldecode($file);
}


/**
 * return true if we have to display the codendi header
 * and footer around the viewvc output
 */
function viewvc_utils_display_header() {
  $request_uri = getStringFromServer('REQUEST_URI');
  $query_string = getStringFromServer('QUERY_STRING');

  if (strpos($request_uri,"view=patch") !== false) return false;
  if (strpos($request_uri,"view=graphimg") !== false) return false;
  if (strpos($request_uri,"annotate=") !== false) return true;
  if (strpos($request_uri,"view=redirect_path") !== false) return false;
  // ViewVC will redirect URLs with "&rev=" to "&revision=". This is needed by Hudson.
  if (strpos($request_uri,"&rev=") !== false) return false;

  if ( strpos($request_uri,"/?") === false && 
       strpos($request_uri,"&r1=") === false &&
       strpos($request_uri,"&r2=") === false &&
       (strpos($request_uri,"view=") === false ||
         strpos($request_uri,"view=co") !== false ) ) {
    return false;
  } else {
    return true;
  }
}  

function viewvc_utils_wrap_utf8_file_name($path) {
    $current_locales = setlocale(LC_ALL, "0");
    setlocale(LC_CTYPE, "en_US.UTF-8");// to allow $path filenames with French characters
    $path = escapeshellarg($path);
    setlocale(LC_ALL, $current_locales);
    return $path;
}

function viewvc_utils_wrap_utf8_exec($command) {
    ob_start();
    putenv("LC_CTYPE=en_US.UTF-8");
    passthru($command);
    return ob_get_clean();
}

/**
 * call the viewvc.cgi and echo the parsed output
 */
function viewvc_utils_passcommand() {
  $parse = viewvc_utils_display_header();
  $request_uri = getStringFromServer('REQUEST_URI');

  //this is very important ...
  if (getStringFromServer('PATH_INFO') == "") {
    $path = "/";
    //echo "no path<br>\n";
  } else {
    $path = getStringFromServer('PATH_INFO');
    // hack: path must always end with /
    if (strrpos($path,"/") != (strlen($path)-1)) $path .= "/";
    //echo "path=$path<br>\n";
  }
  
  // "view=auto" is not well supported in wrapped mode. See SR 341 on Partners.
  $query_string = str_replace("view=auto","view=markup",viewvc_utils_escape_string_from_server('QUERY_STRING'));

  $command = 'HTTP_COOKIE='.viewvc_utils_escape_string_from_server('HTTP_COOKIE').' '.
           'HTTP_USER_AGENT='.viewvc_utils_escape_string_from_server('HTTP_USER_AGENT').' '.
           'REMOTE_ADDR='.escapeshellarg(HTTPRequest::instance()->getIPAddress()).' '.
           'QUERY_STRING='.$query_string.' '.
           'SERVER_SOFTWARE='.viewvc_utils_escape_string_from_server('SERVER_SOFTWARE').' '.
           'SCRIPT_NAME='.viewvc_utils_escape_string_from_server('SCRIPT_NAME').' '.
           'HTTP_ACCEPT_ENCODING='.viewvc_utils_escape_string_from_server('HTTP_ACCEPT_ENCODING').' '.
           'HTTP_ACCEPT_LANGUAGE='.viewvc_utils_escape_string_from_server('HTTP_ACCEPT_LANGUAGE').' '.
           'PATH_INFO='.viewvc_utils_wrap_utf8_file_name($path).' '.
           'PATH='.viewvc_utils_escape_string_from_server('PATH').' '.
           'HTTP_HOST='.viewvc_utils_escape_string_from_server('HTTP_HOST').' '.
           'DOCUMENT_ROOT='.viewvc_utils_escape_string_from_server('DOCUMENT_ROOT').' '.
           'CODENDI_LOCAL_INC='.viewvc_utils_escape_string_from_server('CODENDI_LOCAL_INC').' '.
           '/var/www/cgi-bin/viewvc.cgi 2>&1';

  $content = viewvc_utils_wrap_utf8_exec($command);
  list($headers, $body) = http_split_header_body($content);

  // Set content type header from the value set by ViewVC
  // No other headers are generated by ViewVC because generate_etags
  // is set to 0 in the ViewVC config file
  $found = false;
  $line = strtok($content,"\n\t\r\0\x0B");
  $pathInfo = pathinfo($path);
  while ($line && !$found) {
	if (preg_match('/^Content-Type:(.*)$/',$line,$matches)) {
	    //Until Apache will support the office 2007 mime types by default
        //We should keep test on extension for IE to set the right Mime Type.
        $officeEtensions = array('docm' => 'application/vnd.ms-word.document.macroEnabled.12',
                                 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
                                 'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
                                 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
                                 'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
                                 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
                                 'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
                                 'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
                                 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
                                 'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
                                 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
                                 'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
                                 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
                                 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
                                 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
                                 'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
                                 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                                 'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
                                 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template');
	    if (array_key_exists('extension', $pathInfo) && array_key_exists($pathInfo['extension'], $officeEtensions)) {
            $viewvc_content_type = $officeEtensions[$pathInfo['extension']];
        } else {
            $viewvc_content_type=$matches[1];
        }
 		$found = true;
 	}
	$line = strtok("\n\t\r\0\x0B");	
  }
  $content = substr($content, strpos($content,$line));
  // Now look for 'Location:' header line (e.g. generated by 'view=redirect_pathrev'
  // parameter, used when browsing a directory at a certain revision number)
  $found = false;
  $line = strtok($content,"\n\t\r\0\x0B");
  $viewvc_location = false;
  while ($line && !$found && strlen($line)>1 ) {
        if (preg_match('/^Location:(.*)$/',$line,$matches)) {
                $viewvc_location=$matches[1];
                $found = true;
        }
        $line = strtok("\n\t\r\0\x0B");
  }
  if ($found) $content = substr($content, strpos($content,$line));
  if ($parse) {
    //parse the html doc that we get from viewvc.
    //remove the http header part as well as the html header and
    //html body tags
    $begin_body = strpos($content,"<body");

    if ($begin_body === false) {
      $begin_body = strpos($content,"<BODY");
      $begin_doc = strpos($content,">",$begin_body)+1;
    } else {
      $begin_doc = strpos($content,">",$begin_body)+1;
    }
    $length = strpos($content, "</body>\n</html>") - $begin_doc;
   
    // Now insert references, and display
    echo util_make_reference_links(substr($content,$begin_doc,$length),$GLOBALS['group_id']);

  } else {
    if ($viewvc_location) {
	header('Location: '.$viewvc_location);
	exit(1);
    }
    header('Content-Type:' . $viewvc_content_type.'; charset=utf-8');
    echo $body;
  }
}

function viewvc_utils_escape_string_from_server($key) {
    $string = getStringFromServer($key);

    return escapeshellarg($string);
}

function viewvc_utils_track_browsing($group_id, $type) {
  $query_string = getStringFromServer('QUERY_STRING');
  $request_uri = getStringFromServer('REQUEST_URI');

  if (strpos($query_string,"view=markup") !== FALSE ||
      strpos($query_string,"view=auto") !== FALSE ||
      strpos($request_uri,"*checkout*") !== FALSE ||
      strpos($query_string,"annotate=") !== FALSE) {

    if ($type == 'svn') {
      $browse_column = 'svn_browse';
      $table = 'group_svn_full_history';
    } else if ($type == 'cvs') {
      $browse_column = 'cvs_browse';
      $table = 'group_cvs_full_history';
    } 

    $user_id = user_getid();
    $year   = strftime("%Y");
    $mon    = strftime("%m");
    $day    = strftime("%d");
    $db_day = $year.$mon.$day;

    $sql = "SELECT ".$browse_column." FROM ".$table." WHERE group_id = ".db_ei($group_id)." AND user_id = ".$user_id." AND day = '".$db_day."'";
    $res = db_query($sql);
    if (db_numrows($res) > 0) {
        db_query("UPDATE ".$table." SET ".$browse_column."=".$browse_column."+1 WHERE group_id = ".db_ei($group_id)." AND user_id = ".$user_id." AND day = '".$db_day."'");
    } else {
        db_query("INSERT INTO ".$table." (group_id,user_id,day,".$browse_column.") VALUES (".db_ei($group_id).",".$user_id.",'".$db_day."',1)");
    }
  }
}

?>
